package com.longtop.framework.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.longtop.framework.security.BaseUncheckedException;

public class MapUtils {
	private static Log log = LogFactory.getLog(MapUtils.class);
	private Map map;

	public MapUtils(Map map) {
		this.map = map;
	}

	public MapUtils() {

	}

	public Object get(String key, Object defaultObject) {
		Object mapObject = map.get(key);
		if (mapObject == null) {
			return defaultObject;
		}
		return mapObject;
	}

	public Object get(String key) {
		return get(key, null);
	}

	public String getStr(String key, String defaultString) {
		Object mapObject = map.get(key);
		if (mapObject == null) {
			return defaultString;
		}
		return mapObject.toString();
	}

	public String getStr(String key) {
		return getStr(key, "");
	}

	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public static boolean containsKeyIgnoreCase(Map map, String key) {
		if (map == null || map.size() < 1) {
			return false;
		} else {
			for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
				Entry entry = (Entry) iterator.next();
				if (entry == null) {
					continue;
				}
				String strKey = entry.getKey().toString();
				if (strKey == null || strKey.trim().length() < 1) {
					continue;
				}
				if (strKey.equalsIgnoreCase(key)) {
					return true;
				}
			}
		}
		return false;

	}

	public static String containsKeyIgnoreCaseBySplit(Map map, String key, String strDelim) {
		if (map == null || map.size() < 1) {
			return null;
		} else {
			for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) {
				Entry entry = (Entry) iterator.next();
				if (entry == null) {
					continue;
				}
				String strKey = entry.getKey().toString();
				if (strKey == null || strKey.trim().length() < 1) {
					continue;
				}
				String strSplitValue = "";
				if (strKey.indexOf(strDelim) != -1) {
					strSplitValue = StringUtil.split(strKey, strDelim)[0];
				} else {
					strSplitValue = strKey;
				}
				if (strSplitValue.equalsIgnoreCase(key)) {
					return strKey;
				}
			}
		}
		return null;

	}

	public static Map<String, ?> copyFiledCondition(Object obj, Map<String, ?> conditionMap) throws BaseUncheckedException {
		Map map = new HashMap();
		try {
			map = vo4Map(obj);
		} catch (BaseUncheckedException e) {
			e.printStackTrace();
			throw new BaseUncheckedException(e.getMessage());
		}
		if (conditionMap == null) {
			return map;
		} else {
			if (conditionMap.size() > 0) {
				for (Iterator iterator = conditionMap.entrySet().iterator(); iterator.hasNext();) {
					Entry entry = (Entry) iterator.next();
					if (entry == null) {
						continue;
					}
					String strKey = entry.getKey().toString();
					String strValues = entry.getValue().toString();
					if (strKey == null || strKey.trim().length() < 1 || strValues == null || strValues.trim().length() < 1) {
						continue;
					}
					if (strValues.indexOf(",") != -1) {
						String[] strValues1 = StringUtil.split(strValues, ",");
						for (int i = 0; i < strValues1.length; i++) {
							String strValue = strValues1[i];
							if (containsKeyIgnoreCase(map, strValue)) {
								Object objValue = map.get(strValue);
								map.remove(strValue);
								strValue = strValue + "|" + strKey;
								map.put(strValue, objValue);
							}
						}
					} else {
						String strValue = strValues;
						if (containsKeyIgnoreCase(map, strValue)) {
							Object objValue = map.get(strValue);
							map.remove(strValue);
							strValue = strValue + "|" + strKey;
							map.put(strValue, objValue);
						}
					}

				}

			}

		}
		return map;
	}

	public static Map vo2Map(Object obj, Map map) throws BaseUncheckedException {
		if (map == null) {
			return vo4Map(obj, null);
		} else {
			Map<String, Object> mapBean = new HashMap<String, Object>();
			for (Iterator<Entry<String, Object>> iterator = map.entrySet().iterator(); iterator.hasNext();) {
				Entry<String, Object> entry = iterator.next();
				String key = entry.getKey();

				Object objValue = ReflectUtil.getFieldValue(obj, entry.getKey());
				mapBean.put(key, objValue);
			}
			return mapBean;
		}

	}

	public static Map vo4Map(Object obj) throws BaseUncheckedException {
		return vo4Map(obj, null);
	}

	public static Map vo4Map(Object obj, String except) throws BaseUncheckedException {
		if (obj instanceof Map) {
			return (Map) obj;
		} else {
			Map mapBean = new HashMap();
			Class clz = obj.getClass();
			Field[] field1 = clz.getDeclaredFields();
			Field[] field2 = clz.getSuperclass().getDeclaredFields();
			Field[] field = new Field[field1.length + field2.length];
			for (int i = 0; i < field1.length; i++) {
				field[i] = field1[i];
			}
			for (int i = 0; i < field2.length; i++) {
				field[field1.length + i] = field2[i];
			}

			for (int i = 0; i < field.length; i++) {
				String fieldName = field[i].getName();
				if (except != null && except.trim().length() > 0) {
					if (except.indexOf(",") != -1) {
						String[] strSplit = StringUtil.split(except, ",");
						for (int jj = 0; jj < strSplit.length; jj++) {
							if (fieldName.equalsIgnoreCase(strSplit[jj])) {
								continue;
							}
						}
					} else {
						if (fieldName.equalsIgnoreCase(except)) {
							continue;
						}
					}
				}
				String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
				Method method = null;
				try {
					method = clz.getMethod(methodName, null);
				} catch (SecurityException e) {
					e.printStackTrace();
					throw new BaseUncheckedException("发生异常");
				} catch (NoSuchMethodException e) {
					methodName = "is" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
					try {
						method = clz.getMethod(methodName, null);
					} catch (SecurityException e1) {
						e.printStackTrace();
						throw new BaseUncheckedException("发生异常");
					} catch (NoSuchMethodException e1) {
						log.error("传入对象的" + fieldName + "属性没有get方法");
						continue;
					}

				}
				Object objReturn = null;
				if (method != null) {
					try {
						objReturn = method.invoke(obj, null);
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
						throw new BaseUncheckedException("发生异常");
					} catch (IllegalAccessException e) {
						e.printStackTrace();
						throw new BaseUncheckedException("发生异常");
					} catch (InvocationTargetException e) {
						e.printStackTrace();
						throw new BaseUncheckedException("发生异常");
					}
					mapBean.put(fieldName, objReturn);
				}

			}
			return mapBean;
		}
	}

	public static org.apache.commons.collections.MapUtils getApacheMapUtils() {
		return new org.apache.commons.collections.MapUtils();
	}
}
